function curMouseEvent(): MouseEvent {
	// eslint-disable-next-line dot-notation
	return egret['web'].WebTouchHandler.curMouseEvent || {};
}

// egret的timer没有做保护，虽然是once的，但是一旦错误会不停的调用下去，所以最好不用
function nextFrame(listener: () => void, thisObject?: any): void {
	MainWindow.ins().nextFrame(listener, thisObject);
}

function nextFrameNum(frame: number, listener: () => void, thisObject: any): void {
	MainWindow.ins().nextFrameNum(frame, listener, thisObject);
}

function addChildBeforeName(
	f: egret.DisplayObjectContainer,
	mc: egret.DisplayObject,
	name: egret.DisplayObject,
) {
	if (name) {
		const index = f.getChildIndex(name);
		if (index >= 0) {
			f.addChildAt(mc, index);
			return;
		}
	}
	f.addChildAt(mc, 0);
}

function addChildAfterName(
	f: egret.DisplayObjectContainer,
	mc: egret.DisplayObject,
	name: egret.DisplayObject,
) {
	if (name) {
		const index = f.getChildIndex(name);
		if (index >= 0) {
			f.addChildAt(mc, index + 1);
			return;
		}
	}
	f.addChild(mc);
}

// function nextSec(sec: number, listener: Function, thisObject: any): void {

// }

function isVisible(obj: egret.DisplayObject): boolean {
	if (!obj.stage) {
		return false;
	}
	while (obj) {
		if (!obj.visible) {
			return false;
		}
		obj = obj.parent;
		if (obj === obj.stage) {
			break;
		}
	}
	return true;
}

function getScaleToGlobal(obj: egret.DisplayObject): number[] {
	const p0 = obj.localToGlobal(0, 0);
	const p1 = obj.localToGlobal(1, 1);
	return [p1.x - p0.x, p1.y - p0.y];
}

// return M
function gpuSize() {
	const __tempCache = RES['__tempCache'];
	let totalImageSize = 0;
	for (const key in __tempCache) {
		const img = __tempCache[key];
		if (img instanceof egret.Texture) {
			totalImageSize += img.$bitmapWidth * img.$bitmapHeight * 4;
		}
	}
	return totalImageSize / 1024 / 1024;
}

function getBlendNumber(BlendMode: string): number {
	return BlendMode === egret.BlendMode.NORMAL ? 0 : 1;
}

function getBlendString(BlendMode: number): string {
	return BlendMode === 0 ? egret.BlendMode.NORMAL : egret.BlendMode.ADD;
}

function fitSize(con: egret.DisplayObject, w: number, h: number): boolean {
	if (con.width === w && con.height === h) {
		return true;
	}
	return false;
}

function searchSize(con: egret.DisplayObject, w: number, h: number): number {
	let rt = 1;
	if (fitSize(con, w, h)) {
		console.log('find', w, h, con.name);
	}
	if (con instanceof egret.DisplayObjectContainer) {
		for (const c of con.$children) {
			rt += searchSize(c, w, h);
		}
	}
	return rt;
}

function searchStage(w: number, h: number): number {
	const rt = searchSize(MainWindow.ins().stage, w, h);
	console.log('searchStage', w, h, rt);
	return rt;
}

function egretTrace() {
	ui.log({
		sfactor: egret.sys.DisplayList.$canvasScaleFactor,
		sx: egret.sys.DisplayList.$canvasScaleX,
		sy: egret.sys.DisplayList.$canvasScaleY,
	});
}
